﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using rock.BLL;
using rock.UI.common;

namespace rock.UI
{
    public partial class FormUserAccountUpdate : Form
    {
      

        private ModelUSER_ACCOUNT userAccount = new ModelUSER_ACCOUNT();
        private ModelUSER_AUTHO userAutho = new ModelUSER_AUTHO();
        public ModelUSER_ACCOUNT _userAccount
		{
            set { userAccount = value; }
            get { return userAccount; }
		}

      
        public ModelUSER_AUTHO _userAutho
        {
            set {
                    if (value == null)
                    {
                        userAutho = new ModelUSER_AUTHO();
                    }
                    else {
                        userAutho = value;
                    }
                
                }
            get { return userAutho; }
        }

        DalUSER_ACCOUNT dalUSER_ACCOUNT = new DalUSER_ACCOUNT();
        DalUSER_AUTHO dalUSER_AUTHO = new DalUSER_AUTHO();
        BllUSER_ACCOUNT bllUSER_ACCOUNT = new BllUSER_ACCOUNT();
        public FormUserAccountUpdate()
        {
            InitializeComponent();
            this.AcceptButton = this.button1;
            this.CancelButton = this.button2;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.WindowState = FormWindowState.Normal;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = global::rock.Properties.Resources.house;
        }

        private void FormUserAccountUpdate_Load(object sender, EventArgs e)
        {
           if (dalUSER_ACCOUNT.Exists(this._userAccount.ACCOUNT))
           {
               this.textBox2account.Enabled = false;
           }

            this.textBox1Username.Text = this._userAccount.USERNAME;
            this.textBox2account.Text = this._userAccount.ACCOUNT;
           
            this.textBox3password.Text = this._userAccount.PASSWORD;
            this.textBox4phone.Text = this._userAccount.PHONE;
            this.textBox5note.Text = this._userAccount.NOTE;



            this.checkBox1baseinfo.Checked = this._userAutho.BASE_AUTHO == 1 ? true : false;
            this.checkBox2report.Checked = this._userAutho.REPORT_AUTHO == 1 ? true : false;
            this.checkBox3Message.Checked = this._userAutho.MESSAGE_AUTHO == 1 ? true : false;
            this.checkBox4House.Checked = this._userAutho.HOUSE_AUTHO == 1 ? true : false;
            this.checkBox5fee.Checked = this._userAutho.FEE_AUTHO == 1 ? true : false;

            this.checkBox2Config.Checked = this._userAutho.CONFIG_AUTHO == 1 ? true : false;
            this.checkBox1Picture.Checked = this._userAutho.PICTURE_AUTHO == 1 ? true : false;
        }
        private Boolean check()
        {
            try
            {
                StringUtils.checkText(this.textBox2account.Text, 2, 30, "登陆帐号");
                StringUtils.checkText(this.textBox3password.Text, 4, 30, "登陆密码");
                StringUtils.checkText(this.textBox1Username.Text, 2, 30, "用户名称");

                StringUtils.checkIsWorld(this.textBox3password.Text,   "登陆密码");
                StringUtils.checkIsWorld(this.textBox2account.Text, "登陆帐号");

            
                

                StringUtils.checkText(this.textBox4phone.Text, 0, 30, "联系电话");
                StringUtils.checkText(this.textBox5note.Text, 0, 300, "备注信息");

                //更新.原值不检测
                if (this._userAccount.ACCOUNT != null &&  dalUSER_ACCOUNT.Exists(this._userAccount.ACCOUNT))
                {
                    if (_userAccount.ACCOUNT.Equals(this.textBox2account.Text.Trim()))
                    {
                        return true;
                    }
                }

                //检查帐号
                String existSql2 = "select count(1) from USER_ACCOUNT where ACCOUNT = '{0}'";
                existSql2 = String.Format(existSql2, this.textBox2account.Text.Trim());
                if (BllCommonUtil.Exists(existSql2))
                {
                    MessageBox.Show("登陆帐号值已存在,请输入其它的值", "提示");
                    return false;
                }
               
                


            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "提示");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!check())
            {
                return;
            }
            this._userAccount.USERNAME = this.textBox1Username.Text;
            this._userAccount.ACCOUNT=  this.textBox2account.Text;
            this._userAccount.PASSWORD = this.textBox3password.Text;
            this._userAccount.PHONE= this.textBox4phone.Text;
            this._userAccount.NOTE=  this.textBox5note.Text;

            this._userAutho.BASE_AUTHO = this.checkBox1baseinfo.Checked  ? 1 : 0;
            this._userAutho.REPORT_AUTHO =  this.checkBox2report.Checked  ? 1 : 0;
            this._userAutho.MESSAGE_AUTHO =   this.checkBox3Message.Checked  ? 1 : 0; ;
            this._userAutho.HOUSE_AUTHO =  this.checkBox4House.Checked  ? 1 : 0;
            this._userAutho.FEE_AUTHO= this.checkBox5fee.Checked  ? 1 : 0;
             this._userAutho.CONFIG_AUTHO= this.checkBox2Config.Checked  ? 1 : 0;
            this._userAutho.PICTURE_AUTHO = this.checkBox1Picture.Checked ? 1 : 0;


            this._userAutho.ACCOUNT = this.textBox2account.Text;


            if (this._userAccount.ACCOUNT.Equals(Constant.SYSTEM_DEFAULT_ACCOUNT)) {
                this._userAutho.BASE_AUTHO = this.checkBox1baseinfo.Checked ? 1 : 1;
                this._userAutho.REPORT_AUTHO = this.checkBox2report.Checked ? 1 : 1;
                this._userAutho.MESSAGE_AUTHO = this.checkBox3Message.Checked ? 1 : 1; ;
                this._userAutho.HOUSE_AUTHO = this.checkBox4House.Checked ? 1 : 1;
                this._userAutho.FEE_AUTHO = this.checkBox5fee.Checked ? 1 : 1;
                this._userAutho.CONFIG_AUTHO = this.checkBox2Config.Checked ? 1 : 1;
                this._userAutho.PICTURE_AUTHO = this.checkBox1Picture.Checked ? 1 : 1;
                MessageBox.Show(Constant.SYSTEM_DEFAULT_ACCOUNT + " 是系统内置帐号.权限默认为所有权限.", "提示");
            }

            bllUSER_ACCOUNT.updateOrAddUser(_userAccount, _userAutho);
            //if (dalUSER_ACCOUNT.Exists(this._userAccount.ACCOUNT))
            //{
            //    dalUSER_ACCOUNT.Update(this._userAccount);
            //}
            //else 
            //{
            //    dalUSER_ACCOUNT.Add(this._userAccount);
            //}

            //if (dalUSER_AUTHO.Exists(this._userAccount.ACCOUNT))
            //{
            //    dalUSER_AUTHO.Update(this._userAutho);
            //}
            //else
            //{
            //    dalUSER_AUTHO.Add(this._userAutho);
            //}
            
            //触发代理
            oneRefreshParentData();
            this.Close();

        }
        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //代理
        public delegate void RefreshParentData();
        public RefreshParentData oneRefreshParentData;
    }
}
